﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class DaoTaoNgoaiNgu
    {
        public String ID;
        public String TenNgoaiNgu;
        public String MaDaoTaoNgoaiNgu;
        public String MaDanhGiaTrinhDo;

        public DanhGiaTrinhDoNgoaiNgu DanhGiaTrinhDo;

        public DanhGiaTrinhDoNgoaiNgu getDanhGiaTrinhDoNgoaiNgu()
        {
            return new DanhGiaTrinhDoNgoaiNgu(MaDanhGiaTrinhDo);
        }
        public DaoTaoNgoaiNgu(String _ID)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM DaoTaoNaiNgu " +
                "WHERE ID = '" + _ID + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = _ID;
                MaDaoTaoNgoaiNgu = row["MaDaoTaoNaiNgu"].ToString();
                MaDanhGiaTrinhDo = row["MaDanhGiaTrinhDo"].ToString();
                TenNgoaiNgu = row["TenNgoaiNgu"].ToString();
            }
        }

        public DaoTaoNgoaiNgu(String _TenNgoaiNgu,DanhGiaTrinhDoNgoaiNgu _DanhGiaTrinhDo)
        {
            ID = Database.getID("DaoTaoNaiNgu", "ID");
            MaDaoTaoNgoaiNgu = Database.getID("DaoTaoNaiNgu", "MaDaoTaoNaiNgu");
            MaDanhGiaTrinhDo = _DanhGiaTrinhDo.MaDanhGiaTrinhDo;
            TenNgoaiNgu = _TenNgoaiNgu;
            //object
            DanhGiaTrinhDo = _DanhGiaTrinhDo;
        }

        public Boolean Save()
        {
            DanhGiaTrinhDo.Save();

            QueryResult result;
            //Insert
            result = Database.shareDatabase.Query("INSERT INTO DaoTaoNaiNgu VALUES(" +
                "'" + ID + "', " +
                "'" + MaDaoTaoNgoaiNgu + "', " +
                "N'" + TenNgoaiNgu + "', " +
                "'" + MaDanhGiaTrinhDo + "')");
            if (result.ReturnCode == 200)
            {
                
                return true;
            }
            else
                return false;
        }
        public Boolean Update()
        {
            QueryResult result;
            if (Database.isDupplicate("DaoTaoNgoaiNgu", "ID", ID))
            {
                //Update
                result = Database.shareDatabase.Query("UPDATE DaoTaoNaiNgu SET " +
                    "TenNgoaiNgu = N'" + TenNgoaiNgu + "' WHERE ID = '" + ID + "'");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
            return false;
        }
        public Boolean Delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM DaoTaoNgoaiNgu " +
                "WHERE ID = '" + ID + "'");
            if (result.ReturnCode == 200)
            {
                DanhGiaTrinhDo.Delete();

                return true;
            }
            else
                return false;
        }

        public static List<DaoTaoNgoaiNgu> getAllDaoTaoNgoaiNgu(String _MaDaoTaoNgoaiNgu)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM DaoTaoNgoaiNgu " +
                "WHERE MaDaoTaoNgoaiNgu = '" + _MaDaoTaoNgoaiNgu + "'");
            if (result.ReturnCode == 200)
            {
                List<DaoTaoNgoaiNgu> _result = new List<DaoTaoNgoaiNgu>();
                foreach (DataRow dtr in result.Table.Rows)
                    _result.Add(new DaoTaoNgoaiNgu(dtr["ID"].ToString()));
                return _result;
            }
            else
                return null;
        }
    }
}
